home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0985.arc
/
ARITH.LTG
< prev
next >
Wrap
Text File
|
1986-02-27
|
5KB
|
190 lines
High-Precision Arithmetic
Listings
Listing 1
multiply(array, factor)
/* multiplies large number in array by factor */
int *array, factor;
{
int carry, product, i, size;
carry = 0;
size = array[0];
for (i = 1; i <= size; i++) {
product = factor * array[i] + carry;
array[i] = product % 10000;
carry = product / 10000;
}
if (carry)
array [array[0] = size+1] = carry;
}
divide(array, divisor)
/* divides large number in array by divisor */
int *array, divisor;
{
int remainder, term, i, size;
remainder = 0;
size = array[0];
for (i = size; i >= 1; i--) {
term = 10000 * remainder + array[i] ;
array[i] = term / divisor;
remainder = term % divisor;
}
if (array[size] == 0)
array [0] = size - 1;
}
add(sumarray, termarray)
/* adds termarray into sumarray */
int *sumarray, *termarray;
{
int carry, sum, i, size;
carry = 0;
if (sumarray[0] < termarray[0])
size = termarray[0];
else
size = sumarray[0];è for (i = 1; i <= size; i++) {
sum = termarray[i] + sumarray[i] + carry;
sumarray[i] = sum % 10000;
carry = sum / 10000;
}
if (carry)
sumarray [sumarray[0] = size+1] = carry;
}
subtract(diffarray, termarray)
/* subtracts termarray from diffarray */
int *diffarray, *termarray;
{
int borrow, diff, i, size;
borrow = 0;
size = diffarray[0];
for (i = 1; i <= size; i++) {
diff = diffarray[i] - termarray[i] + borrow;
borrow = diff / 10000;
diffarray[i] = diff % 10000;
/* kludge needed because a % b is not a true mod */
if (diffarray[i] < 0) {
diffarray[i] += 10000;
borrow--;
}
}
/* difference may be much smaller than either term */
while ((diffarray[size] == 0) && (size > 0))
diffarray[0] = size--;
}
Listing 2
#include "arith.c"
#include <stdio.h>
main()
/* program to compute and print 2^11213-1 */
{
int d[900], f, j, size;
for (j = 1; j < 900; j++)
d[j] = 0;
d[1] = 128;
f = 8192;
d[0] = 1;
for (j = 0; j < 862; j++)
multiply(d, f);
d[1]--;
size = d[0];
for (j = size; j > 0; j--)
printf("%d\n", d[j]);
}
è
Listing 3
#include "arith.c"
#include <stdio.h>
main()
/* program to compute and print e to 100 places */
{
int e[27], t[27], i, f, size;
for ( i = 1; i < 27; i++)
e[i] = t[i] = 0;
e[26] = 2000;
t[26] = 1000;
f = 2;
size = 26;
e[0] = t[0] = size;
do
{
divide(t, f);
add(e, t);
f++;
} while (t[0] > 0);
for (i = size; i > 0; i--)
printf("%d\n", e[i]);
}
Listing 4
#include "arith.c"
#include <stdio.h>
series(array, f, size)
int *array, f, size;
{
int power[100], temp[100], sign, divisor, i;
/* set power equal to 1/f */
for ( i = 1; i < size; i++)
power[i] = 0;
power[size] = 1000;
power[0] = size;
divide(power, f);
sign = 1;
divisor = 3;
for ( i = 0; i <= size; i++)
array[i] = power[i];
do
{
divide(power, f * f);
sign = -sign;
for ( i = 0; i <= size; i++)
temp[i] = power[i];è divide(temp, divisor);
divisor += 2;
if (sign > 0)
add(array, temp);
else
subtract(array, temp);
} while ( temp[0] > 0);
}
main()
/* program to compute and print pi to 100 decimal places */
{
int pi[27], partial[27], i, f, size;
size = 26;
series(pi, 5, size); /* pi = ATAN(1/5) */
multiply(pi, 4); /* pi = pi * 4 */
series(partial, 239, size); /* partial = ATAN(1/239) */
subtract(pi, partial); /* pi = (pi - partial) */
multiply(pi, 4); /* pi = (pi * 4) */
for (i = size; i > 0; i--)
printf("%d\n", pi[i]);
}